/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maze.paint;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import maze.Game;

/**
 *
 * @author loki
 */
public class StringPrinter {
    Image boxBorder;
    
    int x, y;
    int fontSize;
    
    Font font;
    String[] text;
    
    public int middleX(FontMetrics fontMetric, int maxX, int string) {
        return this.middleX(fontMetric, maxX, text[string]);
    }
    public int middleX(FontMetrics fontMetric, int maxX, String string) {
        
        //Calculate x coördinate
        int width = fontMetric.stringWidth(string);
        int middle = ((maxX - x) - width) / 2;

        return middle;
    }
    
    public int middleY(int maxY) {
        //Calculate vertical middle
        return (maxY - text.length*fontSize) / 2;
    }
    
    public StringPrinter(String[] text, int fontSize, int x, int y) {
        String imagePath = "ress/border_menu.png";
        try {
            this.boxBorder = ImageIO.read(new URL(Game.baseURL, imagePath));
        } catch (IOException ex) {
            this.boxBorder = null;
        }
        
        this.text = text;
        this.x = x;
        this.y = y;
        this.fontSize = fontSize;
        
        font = new Font(Font.MONOSPACED, Font.BOLD, fontSize);
    }
    
    public void paintBox(Graphics g, int maxX, int maxY) {
        //In case rendering the image failed.
        if(this.boxBorder != null) {
            g.drawImage(boxBorder, 0, 0, maxX, maxY, null);
            return;
        }
        //Draw upper wall
        g.drawLine(x, y, maxX, y);
        //Draw lower wall
        g.drawLine(x, maxY, maxX, maxY);
        //Draw Left wall
        g.drawLine(x, y, x, maxY);
        //Draw Right Wall
        g.drawLine(maxX, y, maxX, maxY);
    }
    
    public void paint(Graphics g, int maxX, int maxY) {
        FontMetrics fontMetric = g.getFontMetrics(font);
        
        this.paintBox(g, maxX, maxY);
        g.setFont(font);
        
        int middleY = middleY(maxY);
        for(int y = 0; y < text.length; y++) {
            g.drawString(text[y], x + middleX(fontMetric, maxX, y), this.y + middleY + (y + 1)*fontSize);
        }
    }
}
